package com.ruoyi.courseManagement.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.courseManagement.domain.Category;
import com.ruoyi.courseManagement.domain.vo.CategoryVO;
import com.ruoyi.courseManagement.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 课程分类Controller
 * 
 * @author ruoyi
 * @date 2025-06-09
 */
@RestController
@RequestMapping("/courseManagement/category")
public class CategoryController extends BaseController
{
    @Autowired
    private CategoryService categoryService;

    /**
     * 查询课程分类列表
     */
    @PreAuthorize("@ss.hasPermi('courseManagement:category:list')")
    @GetMapping("/list")
    public TableDataInfo list(CategoryVO categoryVO)
    {
        startPage();
        List<CategoryVO> list = categoryService.selectCategoryList(categoryVO);
        return getDataTable(list);
    }

    /**
     * 根据校区id查询分类列表
     */
    @PreAuthorize("@ss.hasPermi('courseManagement:category:listByCampusId')")
    @GetMapping("/listByCampusId")
    public TableDataInfo listByCampusId(@RequestParam Integer campusId)
    {
        System.out.println(campusId);
        List<Category> list = categoryService.selectCategoryListByCampusId(campusId);
        return getDataTable(list);
    }

//    /**
//     * 查询校区列表
//     */
//    @PreAuthorize("@ss.hasPermi('courseManagement:category:campusList')")
//    @GetMapping("/campusList")
//    public TableDataInfo campusList()
//    {
//        List<CampusVO> list = categoryService.selectCampusList();
//        return getDataTable(list);
//    }

    /**
     * 导出课程分类列表
     */
    @PreAuthorize("@ss.hasPermi('courseManagement:category:export')")
    @Log(title = "课程分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CategoryVO categoryVO)
    {
        List<CategoryVO> list = categoryService.selectCategoryList(categoryVO);
        ExcelUtil<CategoryVO> util = new ExcelUtil<CategoryVO>(CategoryVO.class);
        util.exportExcel(response, list, "课程分类数据");
    }

    /**
     * 获取课程分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('courseManagement:category:query')")
    @GetMapping(value = "/{categoryId}")
    public AjaxResult getInfo(@PathVariable("categoryId") Integer categoryId)
    {
        return success(categoryService.selectCategoryByCategoryId(categoryId));
    }

    /**
     * 新增课程分类
     */
    @PreAuthorize("@ss.hasPermi('courseManagement:category:add')")
    @Log(title = "课程分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Category category)
    {
        return toAjax(categoryService.insertCategory(category));
    }

    /**
     * 修改课程分类
     */
    @PreAuthorize("@ss.hasPermi('courseManagement:category:edit')")
    @Log(title = "课程分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Category category)
    {
        return toAjax(categoryService.updateCategory(category));
    }

    /**
     * 删除课程分类
     */
    @PreAuthorize("@ss.hasPermi('courseManagement:category:remove')")
    @Log(title = "课程分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{categoryIds}")
    public AjaxResult remove(@PathVariable Integer[] categoryIds)
    {
        return toAjax(categoryService.deleteCategoryByCategoryIds(categoryIds));
    }
}
